<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>学生登录</title>
    <script type="text/javascript" src="../js/jquerysession.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js">
    </script>
</head>
<body >

<div class="container-fluid">
    <div class="row-fluid" style="margin-top: 50px">
        <div class="col-md-12">
            <div style="float: right;margin-right: 50px">
                <input id="search_input" class="input-medium search-query" type="text" style="height: 32px"/> <button id="btn_search" class="btn btn-primary">查找科室</button>
            </div>

        </div>
    </div>
    <div class="row-fluid">
        <div class="col-md-12">
            <table class="table">
                <thead>
                <tr>
                    <th>
                        编号
                    </th>
                    <th>
                        医生姓名
                    </th>
                    <th>
                        科室
                    </th>

                    <th>
                        剩余就诊人数
                    </th>
                    <th>
                        挂号
                    </th>

                </tr>
                </thead>
                <tbody id="selectTb">
                <tr>

                </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>


</body>


<script>
    var time = new Date().toLocaleString();

    $(function () {
        $.ajax({
                url:"/doctor/findAll",
                type:"POST",
                data: null,
                // async:false,
                success: function(result){
                    if (result.state == 200){
                        var doctor = result.data;
                        $("#selectTb").html('');
                        $.each(doctor,function (i) {
                            $tr = $("<tr/>");
                            $td1 = $("<td/>").text(doctor[i].dc_ID);
                            $td2 = $("<td/>").text(doctor[i].realName);
                            $td3 = $("<td/>").text(doctor[i].clinic);
                            $td4 = $("<td/>").text(doctor[i].access);

                            if(doctor[i].access==0){
                                $td5 = $("<button/>").text("挂号").attr("disabled", true);
                            }else{
                                $td5 = $("<button/>").text("挂号").click(function () {
                                    $.ajax({
                                        url:"/doctor/updateAccess",
                                        type:"POST",
                                        data: {
                                            dc_ID:doctor[i].dc_ID,
                                            access:doctor[i].access-1

                                        },
                                        success: function(result){
                                            console.log(result);
                                        }

                                    });


                                    $.ajax({
                                        url:"/clinicReg/insertCR",
                                        type:"POST",
                                        data: {
                                            st_ID:window.sessionStorage.getItem("student_id"),
                                            dc_ID:doctor[i].dc_ID,
                                        },
                                        // async:false,
                                        success: function(result){
                                            alert("预约成功");
                                        }

                                        });

                                });
                            }




                            $tr.append($td1).append($td2).append($td3).append($td4).append($td5);

                            $("#selectTb").append($tr);
                        });


                    }


                }
            }
        );
    });
    
    $("#btn_search").click(function () {
        $.ajax({
            url:"/doctor/findByClinic",
            type:"POST",
            data: {clinic:$("#search_input").val()},
            success:function(result){
                if (result.state == 200){
                    var doctor = result.data;
                    $("#selectTb").html('');
                    $.each(doctor,function (i) {
                        $tr = $("<tr/>");
                        $td1 = $("<td/>").text(doctor[i].dc_ID);
                        $td2 = $("<td/>").text(doctor[i].realName);
                        $td3 = $("<td/>").text(doctor[i].clinic);
                        $td4 = $("<td/>").text(doctor[i].access);

                        if(doctor[i].access==0){
                            $td5 = $("<button/>").text("挂号").attr("disabled", true);
                        }else{
                            $td5 = $("<button/>").text("挂号").click(function () {
                                $.ajax({
                                    url:"/doctor/updateAccess",
                                    type:"POST",
                                    data: {
                                        dc_ID:doctor[i].dc_ID,
                                        access:doctor[i].access-1

                                    },
                                    success: function(result){
                                        console.log(result);
                                    }

                                });


                                $.ajax({
                                    url:"/clinicReg/insertCR",
                                    type:"POST",
                                    data: {
                                        st_ID:window.sessionStorage.getItem("student_id"),
                                        dc_ID:doctor[i].dc_ID,
                                    },
                                    // async:false,
                                    success: function(result){
                                        alert("预约成功");
                                    }

                                });

                            });
                        }




                        $tr.append($td1).append($td2).append($td3).append($td4).append($td5);

                        $("#selectTb").append($tr);
                    });


                }
                else {
                    $("#selectTb").html('');
                    $("#selectTb").html('<div>result.message</div>');
                }


            }

        })
    });


</script>


<!-- 底部结束 -->

</html>